<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2019/4/28
 * Time: 14:07
 */

namespace app\api\controller\user;


use app\api\controller\BasicUserApi;
use app\store\service\OrderService;
use service\KdniaoService;
use service\MemberService;
use think\Db;

class Supplier extends BasicUserApi
{
    /**
     * 查询我的会员
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function user(){
        //查询我的会员
        $page_now=input('get.page_now/d',1);
        $page_size=input('get.page_size/d',10);
        $members=Db::name('store_member')
            ->where('parent_id',UID)
            ->where('status',1)
            ->field('headimg,point,nickname,id')
            ->page($page_now,$page_size)
            ->select();
        return json(['msg'=>'success','data'=>$members]);
    }

    /**
     * 改变用户积分
     * @return \think\response\Json
     */
    function change_point(){
        $get=$this->request->only(['id','point','remark'],'get');
        $validate=new \app\xueao\validate\Member();
        if(false===$validate->scene('changepointsupplier')->check($get)){
            return json(['msg'=>$validate->getError()],500);
        }
        //查询我的积分是否足够
        $point=get_login_info(input('token'),'point');
        if($point<$get['point']){
            return json(['msg'=>'您的积分余额不足'],500);
        }
        //判断是否是自己的会员
        $parent_id=Db::name('store_member')
            ->where('id',$get['id'])
            ->value('parent_id');
        if($parent_id!=UID){
            return json(['msg'=>'您只能操作自己的会员'],500);
        }
        //增加会员的积分  减少经销商的积分
        Db::transaction(function () use ($get){
            $remark='您的经销商于['.date('Y/m/d H:i:s').']为您充值['.$get['point'].']积分('.$get['remark'].')';
            MemberService::log_account_change($get['id'],$get['point'],MemberService::ACCOUNT_LOG_TYPE_PARENT,$remark);
            $remark='您于['.date('Y/m/d H:i:s').']为您的会员['.$get['id'].']充值['.$get['point'].']积分('.$get['remark'].')';
            MemberService::log_account_change(UID,-$get['point'],MemberService::ACCOUNT_LOG_TYPE_CONSUME,$remark);
        });
        return json(['msg'=>'改变成功']);
    }

    /**
     * 订单列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function order_list(){
        $status=input('get.status/d');
        $page_now=input('get.page_now/d',1);
        $page_size=input('get.page_size/d',10);
        //查询自己的会员
        $members=Db::name('store_member')
            ->where('parent_id',UID)
            ->where('status',1)
            ->column('id');
        $db=Db::name('store_order')
            ->alias('o')
            ->join('store_order_goods og','og.order_no=o.order_no')
            ->where('o.mid','in',$members);
        if($status){
            $db->where('o.status',$status);
        }
        $list=$db->field('o.order_no,o.status,og.goods_title,og.goods_logo,og.selling_price,og.number')
            ->page($page_now,$page_size)
            ->select();
        foreach ($list as &$v){
            $v['status_text']=OrderService::STATUS_TEXT[$v['status']];
        }
        return json(['msg'=>'success','data'=>$list]);
    }

    /**
     * 订单详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function order_view(){
        $order_no=input('get.order_no');
        $order=Db::name('store_order')
            ->alias('o')
            ->join('store_order_goods og','og.order_no=o.order_no')
            ->join('store_order_express oe','oe.order_no=o.order_no and oe.type = 0')
            ->where('o.order_no',$order_no)
            ->field('o.order_no,o.create_at,o.status,og.goods_title,og.goods_logo,og.selling_price,og.number,oe.username,oe.phone,oe.province,oe.city,oe.area,oe.address')
            ->find();
        if($order){
            $order['status_text']=OrderService::STATUS_TEXT[$order['status']];
        }
        return json(['msg'=>'success','data'=>$order]);
    }

    /**
     * 订单物流
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function order_logistics(){
        $order_no=$this->request->get('order_no');
        $type=$this->request->get('type',0);
        //查询快递记录
        $orderExpress=Db::name('store_order_express')
            ->where('order_no',$order_no)
            ->where('type',$type)
            ->field('order_no,send_no,send_company_title,send_company_code')
            ->find();
        if(!$orderExpress){
            return json(['msg'=>'快递单查询失败'],404);
        }

        $kdniaoService=KdniaoService::getInstance();

        $param['ShipperCode']=$orderExpress['send_company_code'];
        $param['LogisticCode']=$orderExpress['send_no'];

        $res=$kdniaoService->getOrderTracesByJson($param);
        array_multisort(array_column($res['Traces'],'AcceptTime'),SORT_DESC,$res['Traces']);
        $res['Shipper']=$orderExpress['send_company_title'];
        return json(['msg'=>'success','data'=>$res]);
    }
}